//
//  TwoViewController.m
//  JM
//
//  Created by My Mac on 16/6/3.
//  Copyright © 2016年 My Mac. All rights reserved.
//

#import "TwoViewController.h"

@interface TwoViewController ()

@end

@implementation TwoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"沙发";//标题编辑
    self.view.backgroundColor = [UIColor greenColor];//改UIButton *btn  = [UIButton buttonWithType:UIButtonTypeSystem];
    UIButton *btn  = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    [btn setTitle:@"按下去" forState:UIControlStateHighlighted];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [btn setImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal];
    btn.layer.borderWidth = 1;
    btn.layer.cornerRadius = 6;
    btn.layer.borderColor = [UIColor redColor].CGColor;
    btn.frame = CGRectMake(0, 0, 100, 44);
    btn.center = CGPointMake(self.view.frame.size.width/2, 100);
    
    
    [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(onClick2:) forControlEvents:UIControlEventTouchDown];
    // Do any additional setup after loading the view.
    [self.view addSubview:btn];
    
    UILabel *l = [[UILabel alloc] init];
    l.text = @"UILabel测试";

    [l sizeToFit];
    l.center = CGPointMake(16 + l.frame.size.width/2, 240);
    l.textAlignment = NSTextAlignmentCenter;
    l.tag = 100;
    [self.view addSubview:l];
    
    UISwitch *swi = [[UISwitch alloc] init];
    [swi sizeToFit];
    swi.center = CGPointMake(150, 150);
    swi.onTintColor = [UIColor redColor];
    [self.view addSubview:swi];
    
    [swi addTarget:self action:@selector(onSwitch:) forControlEvents:UIControlEventValueChanged];//UIControlEventValueChanged点一下就改变的方法
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"左对齐", @"居中", @"右对齐"]];
    [seg sizeToFit];
    seg.center = CGPointMake(160, 190);
    [seg addTarget:self action:@selector(onSeg:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:seg];
}

- (void)onSeg:(UISegmentedControl *)seg
{
    UILabel *l = (UILabel *)[self.view viewWithTag:100];
    
        
        if (seg.selectedSegmentIndex == 0) {
            l.center = CGPointMake(16 + l.frame.size.width/2, l.center.y);
        }else if(seg.selectedSegmentIndex == 1){
            l.center = CGPointMake(self.view.frame.size.width/2, l.center.y);
        }else{
            l.center = CGPointMake(self.view.frame.size.width - (16 + l.frame.size.width/2), l.center.y);
        }
}


-(void) onSwitch:(UISwitch *)swi
{
    UILabel *l = (UILabel *)[self.view viewWithTag:100];
    if(swi.isOn){
        l.layer.borderWidth = 1;
    }else
    {
        l.layer.borderWidth = 0;
    }
    
}
-(void) onClick:(UIButton *)sender
{
    sender.backgroundColor  = [UIColor whiteColor];
}
-(void) onClick2:(UIButton *)sender
{
    sender.backgroundColor  = [UIColor grayColor];
}
// Do any additional setup after loading the view.


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
